<?php

date_default_timezone_set('Europe/Vilnius');
include_once './FourInALinePlayer.php';
include_once './Player.php';

class AllTests {

    private $_argv;

    public function __construct() {

    }

    public function test2of5() {
        $player = new Player(1, 6, 2);
        $grid = array(
            array(null, null),
            array(null, null),
            array(1, null),
            array(1, null),
            array(null, null),
            array(null, null)
        );
        $player->setGrid($grid);
        $player->setGameNumber(0);
        echo "Starting test2of5: ";
        $value = $player->checkForTwofFive(1);

        if ($value === -1) {
            $this->msg(false, "value = -1");
        } else if ($value === 1) {
            $this->msg(true);
        } else
            $this->msg(false, "value unexpected value=$value");
    }

    /* check if player placed marker more to the center rather than side */

    public function test2of5nearCeanter() {
        $player = new Player(1, 10, 2);
        $grid = array(
            array(null, null),
            array(null, null),
            array(1, null),
            array(1, null),
            array(null, null),
            array(null, null),
            array(null, null),
            array(null, null),
            array(null, null),
            array(1, null)
        );
        $player->setGrid($grid);
        $player->setGameNumber(0);
        echo "Starting test2of5: ";
        $value = $player->checkForTwofFive(1);

        if ($value === -1) {
            $this->msg(false, "value = -1");
        } else if ($value === 4) {
            $this->msg(true);
        } else
            $this->msg(false, "value unexpected value=$value");
    }

    public function test3of4diagonal() {
        $player = new Player(1, 10, 6);
        $grid = array(
            array(null, null, null, null, null, null),
            array(1, null, null, null, null, null),
            array(1, 1, null, null, null, null),
            array(2, 2, 1, null, null, null),
            array(2, 1, null, null, null, null),
            array(null, null, null, null, null, null),
            array(1, 2, 2, null, null, null),
            array(2, 2, 1, null, null, null),
            array(2, 1, null, null, null, null),
            array(1, null, null, null, null, null)
        );
        $player->setGrid($grid);
        $player->setGameNumber(1);
        $player->speak = true;
        echo "Starting test3of4: ";
        $value = $player->checkForThreeOfFour($grid, 1);

        if ($value === -1) {
            $this->msg(false, "value = -1");
        } else if ($value === 6) {
            $this->msg(true);
        } else
            $this->msg(false, "unexpected value=$value");
    }

    public function testMakeMove(){
        $player = new Player(1, 2, 2);
        $grid = array(
            array(null, null),
            array(null, null)
            );
        $player->setGrid($grid);
        $player->makeMove($grid, 1, 1);
        $value = $grid[1][0];
        if ($value === 1) {
            $this->msg(true);
        } else
            $this->msg(false, "unexpected value=$value");

    }

    /* checks if player reacts when new game begins */

    public function testNewGame() {
        $player = new Player(1, 4, 4);
        echo "Testing New Game event empty grid:\n";
        $grid = array(
            array(null, null, null, null),
            array(2, null, null, null),
            array(null, null, null, null),
            array(null, null, null, null));
        $player->setGrid($grid);
        echo "\tmove>=2: ";
        $player->move = 5;
        $this->msg($player->isItNewGame()); //should be new game
        echo "\tmove<1: ";
        $player->move = 1;
        $this->msg(!$player->isItNewGame());

        echo "Testing New Game event NOT empty grid:";
        $grid = array(
            array(null, null, null, null),
            array(2, null, null, null),
            array(1, 2, null, null),
            array(null, null, null, null));
        $player->setGrid($grid);
        $player->move = 2;
        $this->msg(!$player->isItNewGame());
    }

    public function runAll() {
        echo "\n- - - If methods tests - - -\n";
        $this->test2of5();
        $this->test2of5nearCeanter();
        $this->test3of4diagonal();
        echo "\n- - - Misc. tests - - -\n";
        $this->testNewGame();
        $this->testMakeMove();
    }

    private function msg($passed, $mesage="") {
        echo $mesage;
        if ($passed === true)
            echo "\n\t . . . . . . . . . . . . . . . . . passed\n";
        if ($passed === false)
            echo "\n\t . . . . . . . . . . . . . . . . . FAILED\n";
    }

}

$testClass = new AllTests();
echo "Starting All Unit tests..\n";
echo "==============================\n";
$testClass->runAll();
echo "\n==============================\n";
echo "Testing ended\n";
?>
